package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.framework.interceptor.LoginUser;
import com.ruoyi.framework.interceptor.MpAuthorize;
import com.ruoyi.system.domain.SysCompany;
import com.ruoyi.system.domain.SysConsumer;
import com.ruoyi.web.dto.consumer.*;
import com.ruoyi.web.service.consumer.MpConsumerService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;


@Api(tags = "用户端登录/注册")
@RestController
@RequestMapping("/system/consumer")
public class SysConsumerController {

    @Autowired
    private MpConsumerService sysConsumerService;


    @ApiModelProperty(value = "用户信息列表")
    @GetMapping("/list")
    public TableDataInfo list(@RequestParam(value = "keyword", required = false) String keyword,
                              @RequestParam(value = "pageNum", defaultValue = "1") Integer page,
                              @RequestParam(value = "pageSize", defaultValue = "10") Integer limit,
                              @RequestParam(value = "companyId",required = false)Integer companyId) {
        return sysConsumerService.getList(keyword,page,limit,companyId);
    }


    @PreAuthorize("@ss.hasPermi('system:company:add')")
    @Log(title = "用户管理", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public AjaxResult add( @RequestBody  @Valid ConsumerRequest registerRequest)
    {
        return AjaxResult.success(sysConsumerService.save(registerRequest));
    }

    @PreAuthorize("@ss.hasPermi('system:company:update')")
    @Log(title = "用户修改", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/update")
    public AjaxResult update(@RequestBody ConsumerRequest consumer)
    {
        return AjaxResult.success(sysConsumerService.update(consumer));
    }


    @PreAuthorize("@ss.hasPermi('system:company:update')")
    @Log(title = "用户禁用", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/delete")
    public AjaxResult ban(@RequestBody SysConsumer consumer)
    {
        return AjaxResult.success(sysConsumerService.ban(consumer));
    }

    @PreAuthorize("@ss.hasPermi('system:company:delete')")
    @Log(title = "审核用户", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/delete")
    public AjaxResult audit(@RequestBody SysConsumer consumer)
    {
        return AjaxResult.success(sysConsumerService.audit(consumer));
    }

}
